package service;


import dao.Userdao;
import vo.ResultInfo;
import vo.User;

import java.sql.SQLException;

public class Userservice {
    public ResultInfo<User> login(String username, String password) throws SQLException {
        ResultInfo<User> resultInfo = new ResultInfo<>();
        if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
            resultInfo.setCode(0);
            resultInfo.setWhich("账号密码为空");
            return resultInfo;
        }
        Userdao userdao = new Userdao();
        User user = userdao.getUserByUserName(username);
        if (user == null) {
            resultInfo.setCode(0);
            resultInfo.setWhich("账号不存在");
            return resultInfo;
        }
        String userPassword = user.getPassword();
        if (!userPassword.equals(password)) {
            resultInfo.setCode(0);
            resultInfo.setWhich("密码错误");
            return resultInfo;
        }
        resultInfo.setCode(1);
        resultInfo.setResult(user);
        return resultInfo;
    }

    public ResultInfo<User> register(String username, String password) throws SQLException {
        ResultInfo<User> resultInfo = new ResultInfo<>();
        if (username == null || password == null || username.isEmpty() || password.isEmpty()) {
            resultInfo.setCode(0);
            resultInfo.setWhich("账号密码为空");
            return resultInfo;
        }
        Userdao userdao = new Userdao();
        User user = userdao.getUserByUserName(username);
        if(user==null){
            User user1=new User();
            user1.setUsername(username);
            user1.setPassword(password);
            resultInfo.setCode(1);
            resultInfo.setResult(user1);
            return resultInfo;
        }
        resultInfo.setCode(0);
        resultInfo.setWhich("账号已经存在，无法注册");
        return resultInfo;
    }
}

